package com.example.memorycardgame;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.example.memorycardgame.fragment.ProfileFragment;
import com.example.memorycardgame.fragment.CardGame2Fragment;
import com.example.memorycardgame.fragment.CardGame3Fragment;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNav;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNav = findViewById(R.id.bottom_nav);
        // 默认显示个人信息Fragment
        loadFragment(new ProfileFragment(), "profile");

        bottomNav.setOnItemSelectedListener(item -> {
            Fragment fragment = null;
            String tag = "";
            switch (item.getItemId()) {
                case R.id.nav_profile:
                    fragment = new ProfileFragment();
                    tag = "profile";
                    break;
                case R.id.nav_game2:
                    fragment = new CardGame2Fragment();
                    tag = "game2";
                    break;
                case R.id.nav_game3:
                    fragment = new CardGame3Fragment();
                    tag = "game3";
                    break;
            }
            if (fragment != null) {
                loadFragment(fragment, tag);
            }
            return true;
        });
    }

    private void loadFragment(Fragment fragment, String tag) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, fragment, tag);
        ft.commit();
    }
}